package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonDelDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonEditDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonListDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonSaveDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonServiceListDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonDelVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonEditVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonListVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonSaveVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonServiceListVO;
import com.xbongbong.saas.service.WorkOrderPersonService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 工单服务人员管理模块相关controller
 * @author ly
 * @date 2019/3/15
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workOrderPerson")
public class WorkOrderPersonController {
    @Resource
    private WorkOrderPersonService workOrderPersonService;
    /**
     * @author liuyang
     * @date 2019/3/15
     * Description: 工单服务人员列表接口
     * @param workOrderPersonListDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/listAjax",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateList(@RequestBody @Valid WorkOrderPersonListDTO workOrderPersonListDTO, BindingResult br)throws Exception{
        XbbResponse<WorkOrderPersonListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        }else {
            try {
                WorkOrderPersonListVO workOrderPersonListVO = workOrderPersonService.getList(workOrderPersonListDTO);
                response = new XbbResponse<>(workOrderPersonListVO);
            }catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * @author liuyang
     * @date 2019/3/20
     * Description: 获取公司内工单服务接口
     * @param workOrderPersonServiceListDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/getServiceAjax",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String serviceList(@RequestBody @Valid WorkOrderPersonServiceListDTO workOrderPersonServiceListDTO,BindingResult br)throws Exception{
        XbbResponse<WorkOrderPersonServiceListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        }else {
            try {
                WorkOrderPersonServiceListVO workOrderPersonServiceListVO = workOrderPersonService.getServiceAjax(workOrderPersonServiceListDTO);
                response = new XbbResponse<>(workOrderPersonServiceListVO);
            }catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * @author liuyang
     * @date 2019/3/20 17：14
     * Description: 编辑工单服务人员接口
     * @param workOrderPersonEditDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value ="/edit", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String edit(@RequestBody @Valid WorkOrderPersonEditDTO workOrderPersonEditDTO,BindingResult br)throws Exception{
        XbbResponse<WorkOrderPersonEditVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        }else {
            try {
                WorkOrderPersonEditVO workOrderPersonEditVO = workOrderPersonService.getEdit(workOrderPersonEditDTO);
                response = new XbbResponse<>(workOrderPersonEditVO);
            }catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * @author liuyang
     * @date 2019/3/21 11：02
     * Description: 保存接口
     * @param workOrderPersonSaveDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid WorkOrderPersonSaveDTO workOrderPersonSaveDTO,BindingResult br)throws Exception{
        XbbResponse<WorkOrderPersonSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        }else {
            try {
                WorkOrderPersonSaveVO workOrderPersonSaveVO = workOrderPersonService.save(workOrderPersonSaveDTO);
                response = new XbbResponse<>(workOrderPersonSaveVO);
            }catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * @author liuyang
     * @date 2019/3/21 17:22
     * Description: 删除工单服务人员接口
     * @param workOrderPersonDelDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detele(@RequestBody @Valid WorkOrderPersonDelDTO workOrderPersonDelDTO,BindingResult br)throws Exception{
        XbbResponse<WorkOrderPersonDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        }else {
            try{
                WorkOrderPersonDelVO workOrderPersonDelVO = workOrderPersonService.delete(workOrderPersonDelDTO);
                response = new XbbResponse<>(workOrderPersonDelVO);
            }catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
